package com.example.zhongxinjinqiao.myapplication.device;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.zhongxinjinqiao.myapplication.BaseActivity;
import com.example.zhongxinjinqiao.myapplication.R;
import com.example.zhongxinjinqiao.myapplication.util.LButton;

public class DeviceStatusActivity extends BaseActivity {
    private ListView listView;

    public enum ListString {
        DeviceName(0) ,
        Status,
        Power,
        Distance,
        Time,
        FirmVersion,
        LostVoice;
        int value;

        static  int itemCount()
        {
            return LostVoice.getValue () - DeviceName.getValue () + 1;
        }

        private static class Counter
        {
            private static int nextValue = 0;
        }
        ListString() {
            this(ListString.Counter.nextValue);
        }

        ListString(int value){
            this.value = value;
            ListString.Counter.nextValue = value + 1;
        }

        public int getValue()
        {
            return value;
        }

        public  static ListString getEnum(int value)  {
            for (ListString item : ListString.values()){
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }

        String getStr() {
            switch (this) {
                case DeviceName:
                    return "设备名称";
                case Status:
                    return "状态";
                case Power:
                    return "电量";
                case Time:
                    return "绑定时间";
                case Distance:
                    return "实时距离";
                case FirmVersion:
                    return "固件版本";
                case LostVoice:
                    return "断连提示音";
                default:
                    return "";
            }
        }
    }

    private ImageView topImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_status);

        topImageView = findViewById ( R.id.topImageView );
        listView = findViewById(R.id.listview);
        this.addFooterView ();
        this.setAdapter ();
        this.setTitle ( "设备概述" );

    }

    private  void addFooterView() {
        LinearLayout layout = new LinearLayout ( this );
        layout.setOrientation ( LinearLayout.VERTICAL );
        final int height = 50;

        LButton callBtn = new LButton ( this );
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams ( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        param.topMargin = 20;
        param.leftMargin = 10;
        param.rightMargin = 10;
        param.bottomMargin = 20;
        layout.addView ( callBtn, param );
        callBtn.setCornerRadius ( 10 );
        callBtn.setBorderColor ( Color.BLACK );
        callBtn.setBackgroundColor ( Color.WHITE );
        callBtn.setText ( "呼叫设备" );
        callBtn.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {

            }
        } );


        LButton checkBtn = new LButton ( this );
        checkBtn.setBackgroundColor ( Color.WHITE );
        checkBtn.setCornerRadius ( 5 );
        checkBtn.setBorderColor ( Color.BLACK );
        layout.addView ( checkBtn, param );
        checkBtn.setText ( "查看位置" );
        checkBtn.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {

            }
        } );


        LButton unBingBtn = new LButton ( this );
        unBingBtn.setBackgroundColor ( Color.WHITE );
        unBingBtn.setCornerRadius ( 5 );
        unBingBtn.setBorderColor ( Color.BLACK );
        layout.addView ( unBingBtn, param );
        unBingBtn.setText ( "解除绑定" );
        unBingBtn.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {

            }
        } );

        LButton markLostBtn = new LButton ( this );
        markLostBtn.setBackgroundColor ( Color.WHITE );
        markLostBtn.setCornerRadius ( 5 );
        markLostBtn.setBorderColor ( Color.BLACK );
        layout.addView ( markLostBtn, param );
        markLostBtn.setText ( "标记为丢失" );
        markLostBtn.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {

            }
        } );


        listView.addFooterView ( layout );
    }

    private void setAdapter() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return ListString.itemCount ();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                DeviceDescripeItem item;

                if(view != null) {
                    item = (DeviceDescripeItem) view;
                }
                else {
                    item = new DeviceDescripeItem ( getApplicationContext () );
                }

                item.name.setText ( ListString.getEnum ( i ).getStr () );
                item.descripe.setText ( "hello");

                return item;
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }

    class DeviceDescripeItem extends ConstraintLayout {
        public TextView name;
        public TextView descripe ;
        public DeviceDescripeItem(Context context) {
            super ( context );
            this.initUi ();
        }

        private void initUi() {
            name = new TextView ( getContext () );
            name.setId ( name.hashCode () );
            this.addView ( name );

            ConstraintSet set = new ConstraintSet();
            set.connect ( name.getId (), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 20 );
            set.connect ( name.getId (), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 20 );
            set.connect ( name.getId (), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 10 );
            set.constrainHeight ( name.getId (), ConstraintSet.WRAP_CONTENT );
            set.constrainWidth ( name.getId (), ConstraintSet.WRAP_CONTENT );

            descripe = new TextView ( getContext () );
            this.addView ( descripe );
            set.connect ( descripe.getId (), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 10 );
            set.constrainHeight ( descripe.getId (), ConstraintSet.WRAP_CONTENT );
            set.constrainWidth ( descripe.getId (), ConstraintSet.WRAP_CONTENT );
            set.centerVertically ( descripe.getId (), name.getId () );
            set.applyTo ( this );

        }





        public DeviceDescripeItem(Context context, AttributeSet attrs) {
            super ( context, attrs );
        }

        public DeviceDescripeItem(Context context, AttributeSet attrs, int defStyleAttr) {
            super ( context, attrs, defStyleAttr );
        }
    }
}
